package GUI_myroot;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class MyFrame2 {
    public static void main(String[] args) {
        JFrame jFrame=new JFrame();
        jFrame.setSize(500,500);
        jFrame.setTitle("演示标题");
        jFrame.setAlwaysOnTop(true);//置顶
        jFrame.setLocationRelativeTo(null);//居中
        jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        jFrame.setLayout(null);//取消默认居中位置,只有取消了才会按照xy轴形式添加组件

        //第一个按钮
        JButton jbt=new JButton("按x钮");
        jbt.setBounds(0,0,200,100);//按钮位置大小
        //jbt.组件对象,
        //addActionListener:表示我要给组件添加哪个事件监听（动作监听包含鼠标左键点击，空格)
        jbt.addActionListener(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.out.println("点击点击");
            }
        });

        //第二个按钮
        JButton jbt2=new JButton("二");
        jbt2.setBounds(100,0,200,100);
        jbt2.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                System.out.println("单击");
            jbt2.setBackground(Color.black);
            
            }

            @Override
            public void mousePressed(MouseEvent e) {
                System.out.println("按下不松");
                jbt2.setBackground(Color.green);
            }

            @Override
            public void mouseReleased(MouseEvent e) {
                System.out.println("松开");
                jbt2.setBackground(Color.yellow);
            }

            @Override
            public void mouseEntered(MouseEvent e) {
                System.out.println("划入");
                jbt2.setBackground(Color.MAGENTA);
            }

            @Override
            public void mouseExited(MouseEvent e) {
                System.out.println("移出");
                jbt2.setBackground(Color.ORANGE);
            }
        });

        //添加到界面中
//        jFrame.getContentPane().add(jbt);
        jFrame.getContentPane().add(jbt2);


        jFrame.setVisible(true);
    }

}
